/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.ingenic.glass.camera;

import com.ingenic.glass.camera.pqylive.PqyLiveModule;

import com.ingenic.glass.camera.util.Util;
import com.ingenic.glass.camera.util.A2DPUtil;

import android.util.Log;
import android.app.Application;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.app.Activity;
import android.os.SystemProperties;
import android.os.Build;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.os.Handler;
import android.os.Message;

public class CameraAppImpl extends Application {
    public static final boolean DEBUG = true;
    public static final String TAG = "CameraAppImpl";

    public static final boolean NO_SCREEN_DEVICE = ("true".equals(SystemProperties.get("ro.device.noscreen")));
    public static final boolean HANLANG_THUMBNAIL = SystemProperties.getBoolean("ro.thumbnail.version.hanlang", false);
    public static final boolean USE_TTS = SystemProperties.getBoolean("ro.device.usetts", true);//default: true
    public static final boolean PENQUANYUN_LIVE = false;//区分喷泉云直播
    public static boolean IS_CRUISE_BOARD = false;

    public static final int PICTURE = 0;
    public static final int VIDEO = 1;
    public static final int OTHER = 2;
    public static final int CAMERA_ERROR_PREVIEW = 2;
    public static final int CAMERA_ERROR_TAKEPHOTO = 3;
    public static final int CAMERA_ERROR_RECORD = 4;
    public static final int CAMERA_ERROR_QUICKCAPTURE_HAL_STORE = 5;
    private static final int COLDWAVE_LOWEST_BATTERY_VOLTAGE = 3350;
    private static final int CRUISE_LOWEST_BATTERY_VOLTAGE = 3500;
    public static int LOWEST_BATTERY_VOLTAGE = COLDWAVE_LOWEST_BATTERY_VOLTAGE;//default
    public static final String SCAN_EXTERNAL_URI = "file:///storage/emulated/0";
    public static final int SORT_DESCENDING = 2;

    // 1.没有屏幕
    public static final int NO_SCREEN = 0x00;
    // 2. 无屏直播
    public static final int NO_SCREEN_LIVE = 0x10;
    // 3. 快拍模式
    public static final int CAPTURE = 0x08;
    // 4. 正常显示
    public static final int NORMAL_DISPLAY = 0x0c;
    // 5. 正常显示直播
    public static final int NORMAL_DISPLAY_LIVE = 0x1c;
    // 6. 低功耗显示拍照
    public static final int LOW_POWER_DISPLAY_CAPTURE = 0x0e;
    // 7. 低功耗显示录像
    public static final int LOW_POWER_DISPLAY_VIDEO = 0x0f;
    // 8. 低功耗显示直播
    public static final int LOW_POWER_DISPLAY_VIDEO_LIVE = 0x1f;


    // 1. 没有日期时间水印 
    public static final int NO_WATER_MARK = 0x00;
    // 2. 只有照片添加日期时间水印
    public static final int PICTURE_DATA_TIME_WATER_MARK = 0x03;
    // 3. 只有照片添加日期时间水印和蓝牙名称水印
    public static final int PICTURE_DATA_TIME_AND_BT_NAME_WATER_MARK = 0x07;
	
    // 4. 只有视频添加日期时间水印
    public static final int VIDEO_DATA_TIME_WATER_MARK = 0x09;
    // 5. 只有视频添加日期时间水印和蓝牙名称水印
    public static final int VIDEO_DATA_TIME_AND_BT_NAME_WATER_MARK = 0x19;

    // 6. 照片和视频添加日期时间水印
    public static final int PICTURE_VIDEO_DATA_TIME_WATER_MARK = 0x0b;
    // 7. 照片和视频添加日期时间水印和蓝牙名称水印
    public static final int PICTURE_VIDEO_DATA_TIME_AND_BT_NAME_WATER_MARK = 0x1f;

    /* 直播 */
    public static final int LIVE_VIDEO_ONLY = 0;// default
    public static final int LIVE_AUDIO_ONLY = 1;
    public static final int LIVE_VIDEO_AND_AUDIO = 3;

    private final int MSG_RELEASE_WAKE_LOCK = 1;

    private WakeLock mWakeLock = null;
    @Override
    public void onCreate() {
    super.onCreate();
	Log.d(TAG,"onCreate "+Build.BOARD);
	Storage.initialize(this);
	Util.initialize(this);
	if ("cruise".equalsIgnoreCase(Build.BOARD) || "cruise_ums".equalsIgnoreCase(Build.BOARD)) {
		IS_CRUISE_BOARD = true;
		LOWEST_BATTERY_VOLTAGE = CRUISE_LOWEST_BATTERY_VOLTAGE;
	}

	PowerManager manager = ((PowerManager) getSystemService(POWER_SERVICE));  
	mWakeLock = manager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,"camera");
	A2DPUtil.getInstance(this);

	PqyLiveModule.getInstance(this);
    }

    public void showDialog(final Activity activity,int testResId) {
	AlertDialog.Builder builder= new AlertDialog.Builder(activity);
	builder.setMessage(testResId);
	builder.setCancelable(false);
	builder.setPositiveButton(R.string.positive,new DialogInterface.OnClickListener() {	
		
		@Override
		    public void onClick(DialogInterface dialog, int arg1) {
		    dialog.dismiss();
		    activity.finish();
		}
	    });

	builder.create().show();
    }

        private Handler mHandler = new Handler(){
		@Override
		public void handleMessage(Message msg) {
		    switch(msg.what) {
		    case MSG_RELEASE_WAKE_LOCK:
			if (mWakeLock.isHeld())
			    mWakeLock.release();
			break;
		    }
		}
	    };

	public void acquireWakeLock() {
		Log.d(TAG,"Wakelock has acquire");
		mWakeLock.acquire();
	}

        public void releaseWakeLock() {
		Log.d(TAG,"WakeLock has release");
		mHandler.sendEmptyMessage(MSG_RELEASE_WAKE_LOCK);
	} 

        public void releaseWakeLock(int timeout) {
		Log.d(TAG,"releaseWakeLock :: timeout=" +timeout);
		mHandler.sendEmptyMessageDelayed(MSG_RELEASE_WAKE_LOCK, timeout);
	}

        public static int getWaterMarkMode(int pic_watermark, int video_watermark) {
		Log.d(TAG,"pic_watermark = " + pic_watermark + " video_watermark = " + video_watermark);
		if (pic_watermark == 1 && video_watermark == 1) {
			return PICTURE_VIDEO_DATA_TIME_AND_BT_NAME_WATER_MARK;
		} else if(pic_watermark == 1 && video_watermark == 0) {
			return PICTURE_DATA_TIME_AND_BT_NAME_WATER_MARK;
		} else if (pic_watermark == 0 && video_watermark == 1) {
			return VIDEO_DATA_TIME_AND_BT_NAME_WATER_MARK;
		}   
		return NO_WATER_MARK; 
	}
}